//
//  L0Section.m
//  L0EntryTable
//
//  Created by ∞ on 18/07/08.
//  Copyright 2008 Emanuele Vulcano. All rights reserved.
//

#import "L0EntrySection.h"
#import "L0EntryCell.h"
#import "L0EntryTableController.h"

@implementation L0EntrySection

- (id) init {
	if (self = [super init])
		cellsStorage = [NSMutableArray new];
	
	return self;
}

- (void) dealloc {
	[cellsStorage release];
	[super dealloc];
}

@synthesize title, headerView, footerView, controller;

- (NSUInteger) countOfCells {
	return [cellsStorage count];
}

- (L0EntryCell*) objectInCellsAtIndex:(NSUInteger) idx {
	return [cellsStorage objectAtIndex:idx];
}

- (void) insertObject:(L0EntryCell*) cell inCellsAtIndex:(NSUInteger) idx {
	[cellsStorage insertObject:cell atIndex:idx];
	cell.section = self;
	
	if (self.controller.onscreen) [self.controller.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:idx inSection:[self.controller indexOfObjectInSections:self]]] withRowAnimation:UITableViewRowAnimationFade];
}

- (void) insertObjects:(NSArray*) cells inCellsStartingAtIndex:(NSUInteger) idx {
	NSIndexSet* indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(idx, [cells count])];
	[cellsStorage insertObjects:cells atIndexes:indexes];
	
	NSUInteger section = [self.controller indexOfObjectInSections:self];

	if (self.controller.onscreen) {
		NSMutableArray* paths = [NSMutableArray array];
		NSUInteger len = [cells count];
		for (NSUInteger i = 0; i < len; i++)
			[paths addObject:[NSIndexPath indexPathForRow:i + idx inSection:section]];
		
		[self.controller.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
	}
}

- (void) removeObjectFromCellsAtIndex:(NSUInteger) idx {
	L0EntryCell* cell = [cellsStorage objectAtIndex:idx];
	cell.section = nil;
	
	[cellsStorage removeObjectAtIndex:idx];
	if (self.controller.onscreen) [self.controller.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:idx inSection:[self.controller indexOfObjectInSections:self]]] withRowAnimation:UITableViewRowAnimationFade];
}

- (void) select:(L0EntryCell*) cell {
	NSUInteger idx = [cellsStorage indexOfObject:cell];
	NSAssert(idx != NSNotFound, @"You must use a cell that is actually contained in the cells key of this section.");
	
	[self.controller selectCellAtIndex:idx inSection:self];
}

- (void) selectCellDisplayingEditorAfter:(L0EntryCell*) cell {
	NSUInteger idx = [cellsStorage indexOfObject:cell];
	NSAssert(idx != NSNotFound, @"You must use a cell that is actually contained in the cells key of this section.");
	
	[self selectCellDisplayingEditorAfterCellAtIndex:idx];
}

- (void) selectCellDisplayingEditorAfterCellAtIndex:(NSUInteger) idx {
	idx++;
	while (idx < [cellsStorage count]) {
		L0EntryCell* cell = [self objectInCellsAtIndex:idx];
		if (cell.displaysEditorOnSetUp || cell.displaysKeyboardOnSetUp) {
			[cell select];
			return;
		}
		
		idx++;
	}
	
	NSUInteger section = [self.controller indexOfObjectInSections:self];
	section++;
	if (section >= [self.controller countOfSections])
		[self.controller deselect];
	else {
		L0EntrySection* nextSection = [self.controller objectInSectionsAtIndex:section];
		[nextSection selectCellDisplayingEditorAfterCellAtIndex:0];
	}
}

@end
